<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	$GLOBALS['OUTPUT']->master_section = 'settings';
	$GLOBALS['OUTPUT']->minor_section = $GLOBALS['LANG']->lang_admin->configuration;
	$GLOBALS['OUTPUT']->cpmenu = $menus[$GLOBALS['OUTPUT']->master_section];
	
	$forms = array(	'generalsettings'=>array($GLOBALS['LANG']->lang_admin->generalsettings,new form(),),
					'login_page_news'=>array($GLOBALS['LANG']->lang_admin->login_page_news,new form()),
					'accountsettings'=>array($GLOBALS['LANG']->lang_admin->accountsettings,new form()),
					'timedatesettings'=>array($GLOBALS['LANG']->lang_admin->timedatesettings,new form()),
					'maintenancesettings'=>array($GLOBALS['LANG']->lang_admin->maintenancesettings,new form()),
					'emailsettings'=>array($GLOBALS['LANG']->lang_admin->emailsettings,new form()),
					'advancedsettings'=>array($GLOBALS['LANG']->lang_admin->advancedsettings,new form()),);
					
	$forms['generalsettings'][1]->add_field('text','site_name',$GLOBALS['LANG']->lang_admin->site_name);
	$forms['generalsettings'][1]->add_default('site_name',$settings['site_name']);
	$forms['generalsettings'][1]->add_field('text','admin_email',$GLOBALS['LANG']->lang_admin->admin_email);
	$forms['generalsettings'][1]->add_default('admin_email',$settings['admin_email']);
	$forms['generalsettings'][1]->add_field('text','help_url',$GLOBALS['LANG']->lang_admin->help_url);
	$forms['generalsettings'][1]->add_default('help_url',$settings['help_url']);
	$forms['generalsettings'][1]->add_field('text','num_previous_msgs_init',$GLOBALS['LANG']->lang_admin->num_previous_msgs_init);
	$forms['generalsettings'][1]->add_default('num_previous_msgs_init',$settings['num_previous_msgs_init']);
	
	$forms['login_page_news'][1]->add_field('text','login_page_news',$GLOBALS['LANG']->lang_admin->login_page_news);
	$forms['login_page_news'][1]->add_default('login_page_news',$settings['login_page_news']);
	
	$forms['accountsettings'][1]->add_field('checkbox','allow_guests',$GLOBALS['LANG']->lang_admin->allow_guests);
	$forms['accountsettings'][1]->add_default('allow_guests',$settings['allow_guests']);
	$forms['accountsettings'][1]->add_field('checkbox','allow_registration',$GLOBALS['LANG']->lang_admin->allow_registration);
	$forms['accountsettings'][1]->add_default('allow_registration',$settings['allow_registration']);
	$forms['accountsettings'][1]->add_field('select','account_activation',$GLOBALS['LANG']->lang_admin->account_activation,'',array($GLOBALS['LANG']->lang_admin->noneact=>'none',$GLOBALS['LANG']->lang_admin->useract=>'user',$GLOBALS['LANG']->lang_admin->adminact=>'admin'));
	$forms['accountsettings'][1]->add_default('account_activation',$settings['account_activation']);
	
	$forms['timedatesettings'][1]->add_field('text','long_date_format',$GLOBALS['LANG']->lang_admin->account_activation);
	$forms['timedatesettings'][1]->add_default('long_date_format',$settings['long_date_format']);
	
	$forms['maintenancesettings'][1]->add_field('checkbox','disable_chat',$GLOBALS['LANG']->lang_admin->disable_chat);
	$forms['maintenancesettings'][1]->add_default('disable_chat',$settings['disable_chat']);
	
	$forms['emailsettings'][1]->add_field('checkbox','send_welcome_email',$GLOBALS['LANG']->lang_admin->send_welcome_email);
	$forms['emailsettings'][1]->add_default('send_welcome_email',$settings['send_welcome_email']);
	$forms['emailsettings'][1]->add_field('checkbox','use_smtp',$GLOBALS['LANG']->lang_admin->use_smtp);
	$forms['emailsettings'][1]->add_default('use_smtp',$settings['use_smtp']);
	$forms['emailsettings'][1]->add_field('text','smtp_host',$GLOBALS['LANG']->lang_admin->smtp_host);
	$forms['emailsettings'][1]->add_default('smtp_host',$settings['smtp_host']);
	$forms['emailsettings'][1]->add_field('text','smtp_port',$GLOBALS['LANG']->lang_admin->smtp_port);
	$forms['emailsettings'][1]->add_default('smtp_port',$settings['smtp_port']);
	$forms['emailsettings'][1]->add_field('text','smtp_user',$GLOBALS['LANG']->lang_admin->smtp_user);
	$forms['emailsettings'][1]->add_default('smtp_user',$settings['smtp_user']);
	$forms['emailsettings'][1]->add_field('text','smtp_pass',$GLOBALS['LANG']->lang_admin->smtp_pass);
	$forms['emailsettings'][1]->add_default('smtp_pass',$settings['smtp_pass']);
	
	$forms['advancedsettings'][1]->add_field('text','online_expire_seconds',$GLOBALS['LANG']->lang_admin->online_expire_seconds);
	$forms['advancedsettings'][1]->add_default('online_expire_seconds',$settings['online_expire_seconds']);
	$forms['advancedsettings'][1]->add_field('text','sync_onlinelist_counts',$GLOBALS['LANG']->lang_admin->sync_onlinelist_counts);
	$forms['advancedsettings'][1]->add_default('sync_onlinelist_counts',$settings['sync_onlinelist_counts']);
	$forms['advancedsettings'][1]->add_field('text','stats_update_speed',$GLOBALS['LANG']->lang_admin->stats_update_speed);
	$forms['advancedsettings'][1]->add_default('stats_update_speed',$settings['stats_update_speed']);
	$forms['advancedsettings'][1]->add_field('text','max_cache_size',$GLOBALS['LANG']->lang_admin->max_cache_size);
	$forms['advancedsettings'][1]->add_default('max_cache_size',$settings['max_cache_size']);
	$forms['advancedsettings'][1]->add_field('text','num_cache_preserve',$GLOBALS['LANG']->lang_admin->num_cache_preserve);
	$forms['advancedsettings'][1]->add_default('num_cache_preserve',$settings['num_cache_preserve']);
	$forms['advancedsettings'][1]->add_field('text','refresh_rate',$GLOBALS['LANG']->lang_admin->refresh_rate);
	$forms['advancedsettings'][1]->add_default('refresh_rate',$settings['refresh_rate']);
	
	if(isset($_GET['updatesettings'])){
		
		function check_limits($setting,$min,$max){
			global $settings, $errors;
			
			$test = x7in::read($setting,'i');
			if($min != null && $min > $test)
				return $min;
			
			if($max != null && $max < $test)
				return $max;
			
			return $test;
		}
	
		$settings['site_name'] = $forms['generalsettings'][1]->get_add_default('site_name');
		$settings['admin_email'] = $forms['generalsettings'][1]->get_add_default('admin_email');
		if(!preg_match('/^[^@]*@[^.]*\..*$/',$settings['admin_email']))
			$forms['generalsettings'][1]->add_error('admin_email',$GLOBALS['LANG']->lang_admin->bad_admin_email);
		$settings['help_url'] = $forms['generalsettings'][1]->get_add_default('help_url');
		$settings['num_previous_msgs_init'] = check_limits('num_previous_msgs_init',0,null);
		$forms['generalsettings'][1]->add_default('num_previous_msgs_init',$settings['num_previous_msgs_init']);
		
		$settings['login_page_news'] = $forms['login_page_news'][1]->get_add_default('login_page_news');
		
		$settings['allow_guests'] = (x7in::read('allow_guests') != '');
		$forms['accountsettings'][1]->add_default('allow_guests',$settings['allow_guests']);
		$settings['allow_registration'] = (x7in::read('allow_registration') != '');
		$forms['accountsettings'][1]->add_default('allow_registration',$settings['allow_registration']);
		$settings['account_activation'] = in_array($_POST['account_activation'],array('user','admin','none')) ? $_POST['account_activation'] : 'none';
		$forms['accountsettings'][1]->add_default('account_activation',$settings['account_activation']);
		
		$settings['long_date_format'] = $forms['timedatesettings'][1]->get_add_default('long_date_format');
		
		$settings['disable_chat'] = (x7in::read('disable_chat') != '');
		$forms['maintenancesettings'][1]->add_default('disable_chat',$settings['disable_chat']);
		
		$settings['send_welcome_email'] = (x7in::read('send_welcome_email') != '');
		$forms['emailsettings'][1]->add_default('send_welcome_email',$settings['send_welcome_email']);
		$settings['use_smtp'] = (x7in::read('use_smtp') != '');
		$forms['emailsettings'][1]->add_default('use_smtp',$settings['use_smtp']);
		$settings['smtp_host'] = $forms['emailsettings'][1]->get_add_default('smtp_host');
		$settings['smtp_port'] = $forms['emailsettings'][1]->get_add_default('smtp_port');
		$settings['smtp_user'] = $forms['emailsettings'][1]->get_add_default('smtp_user');
		$settings['smtp_pass'] = $forms['emailsettings'][1]->get_add_default('smtp_pass');
		
		$settings['online_expire_seconds'] = check_limits('online_expire_seconds',$settings['refresh_rate']/1000+10,60);
		$forms['advancedsettings'][1]->add_default('online_expire_seconds',$settings['online_expire_seconds']);
		$settings['sync_onlinelist_counts'] = check_limits('sync_onlinelist_counts',1,50);
		$forms['advancedsettings'][1]->add_default('sync_onlinelist_counts',$settings['sync_onlinelist_counts']);
		$settings['stats_update_speed'] = check_limits('stats_update_speed',1,900);
		$forms['advancedsettings'][1]->add_default('stats_update_speed',$settings['stats_update_speed']);
		$settings['num_cache_preserve'] = check_limits('num_cache_preserve',30,100);
		$forms['advancedsettings'][1]->add_default('num_cache_preserve',$settings['num_cache_preserve']);
		$settings['max_cache_size'] = check_limits('max_cache_size',$settings['num_cache_preserve']*4,$settings['num_cache_preserve']*20);
		$forms['advancedsettings'][1]->add_default('max_cache_size',$settings['max_cache_size']);
		$settings['refresh_rate'] = check_limits('refresh_rate',1000,30000);
		$forms['advancedsettings'][1]->add_default('refresh_rate',$settings['refresh_rate']);
		
		if($settings['use_smtp']){
			if(empty($settings['smtp_host']))
				$forms['emailsettings'][1]->add_error('smtp_host',$GLOBALS['LANG']->lang_admin->missing_value);
			if(empty($settings['smtp_user']))
				$forms['emailsettings'][1]->add_error('smtp_user',$GLOBALS['LANG']->lang_admin->missing_value);
			if(empty($settings['smtp_pass']))
				$forms['emailsettings'][1]->add_error('smtp_pass',$GLOBALS['LANG']->lang_admin->missing_value);
			if(empty($settings['smtp_port']))
				$forms['emailsettings'][1]->add_error('smtp_port',$GLOBALS['LANG']->lang_admin->missing_value);
		}

		$valid_data = true;
		foreach($forms as $test=>$fdata){
			$valid_data = ($valid_data && !$fdata[1]->has_errors());
		}
		
		if($valid_data){
			$settings['key'] = 'settings';
			$GLOBALS['DATABASE']->put('system',$settings);
			$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->settings_updated;
		}else{
			$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->settings_not_updated;
		}
		
	}
	
	$GLOBALS['OUTPUT']->forms = $forms;
	echo tpl_settings::settings_form();
?>